from django.contrib import admin
from .models import (
    PageView, SearchQuery, UserActivity, 
    SalesReport, PopularBook, TrafficSource,
    ConversionFunnel, ABNTest
)


@admin.register(PageView)
class PageViewAdmin(admin.ModelAdmin):
    list_display = ['path', 'user', 'session_id', 'created_at']
    list_filter = ['created_at']
    search_fields = ['path', 'user__email', 'session_id']
    readonly_fields = ['user', 'created_at']
    
    def has_add_permission(self, request):
        return False


@admin.register(SearchQuery)
class SearchQueryAdmin(admin.ModelAdmin):
    list_display = ['query', 'user', 'results_count', 'has_results', 'created_at']
    list_filter = ['has_results', 'created_at']
    search_fields = ['query', 'user__email']
    readonly_fields = ['user', 'created_at']
    
    def has_add_permission(self, request):
        return False


@admin.register(UserActivity)
class UserActivityAdmin(admin.ModelAdmin):
    list_display = ['user', 'activity_type', 'path', 'created_at']
    list_filter = ['activity_type', 'created_at']
    search_fields = ['user__email', 'path', 'activity_type']
    readonly_fields = ['user', 'created_at']
    
    def has_add_permission(self, request):
        return False


@admin.register(SalesReport)
class SalesReportAdmin(admin.ModelAdmin):
    list_display = ['report_type', 'start_date', 'end_date', 'total_revenue', 'total_orders', 'created_at']
    list_filter = ['report_type', 'created_at']
    search_fields = ['report_type']
    readonly_fields = ['created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('report_type', 'start_date', 'end_date')
        }),
        ('收入统计', {
            'fields': ('total_revenue', 'book_sales', 'shipping_fee')
        }),
        ('订单统计', {
            'fields': ('total_orders', 'completed_orders', 'cancelled_orders', 'refund_orders')
        }),
        ('文件', {
            'fields': ('report_file',),
            'classes': ('collapse',)
        })
    )


@admin.register(PopularBook)
class PopularBookAdmin(admin.ModelAdmin):
    list_display = ['book', 'view_count', 'sales_count', 'popularity_score', 'date']
    list_filter = ['date']
    search_fields = ['book__title']
    readonly_fields = ['book', 'date']


@admin.register(TrafficSource)
class TrafficSourceAdmin(admin.ModelAdmin):
    list_display = ['source', 'medium', 'campaign', 'visits', 'conversions', 'conversion_rate', 'date']
    list_filter = ['source', 'date']
    search_fields = ['source', 'medium', 'campaign']
    readonly_fields = ['date']


@admin.register(ConversionFunnel)
class ConversionFunnelAdmin(admin.ModelAdmin):
    list_display = ['step_name', 'visitors', 'conversion_rate', 'drop_off_rate', 'date']
    list_filter = ['date']
    search_fields = ['step_name']
    readonly_fields = ['date']


@admin.register(ABNTest)
class ABNTestAdmin(admin.ModelAdmin):
    list_display = ['name', 'test_type', 'status', 'start_date', 'end_date', 'winner']
    list_filter = ['status', 'test_type', 'start_date']
    search_fields = ['name', 'description']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'test_type', 'description', 'hypothesis')
        }),
        ('时间设置', {
            'fields': ('start_date', 'end_date')
        }),
        ('状态', {
            'fields': ('status', 'winner')
        }),
        ('统计', {
            'fields': ('visitors_a', 'conversions_a', 'visitors_b', 'conversions_b'),
            'classes': ('collapse',)
        })
    )
